<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="description" content="">
  <meta name="keywords" content="">
  
    <link rel="icon" href="/avatar.jpg">
  
    
  <title>ABP 通知系统数据表结构及源码分析 | 科学君的不科学博客</title>
  <link rel="stylesheet" href="/style.css">
  <link rel="stylesheet" href="/lib/jquery.fancybox.min.css">
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <script type="text/javascript" src="https://tajs.qq.com/stats?sId=65546304" charset="UTF-8"></script>
  <script>
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>
</head>

<body>
  <header>
  <div class="header-container">
    <a class='logo' href="/">
      <span>科学君的不科学博客</span>
    </a>
    <ul class="right-header">
      
        <li class="nav-item">
          
            <a href="/" class="item-link">首页</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/about" class="item-link">关于</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/archives" class="item-link">归档</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/tags" class="item-link">标签</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/atom.xml" class="item-link">FEED 订阅</a>
          
        </li>
      
    </ul>
  </div>
</header>

  <main id='post'>
  <div class="content">
    <article>
      <section class="content markdown-body">
        <h1>ABP 通知系统数据表结构及源码分析</h1>
        <div class='post-meta'>
          <i class="fa fa-calendar" aria-hidden="true"></i>
          <time>2018年04月02日</time>
          
          | <i class="fa fa-folder-open-o" aria-hidden="true"></i> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/blog/">blog</a>
  </div>



          
          
          |
          
          <i class="fa fa-tags" aria-hidden="true"></i>
          
          
  <a href="/tags/#Web 开发" class='tag'>Web 开发</a>

  <a href="/tags/#AspNetBoilerplate" class='tag'>AspNetBoilerplate</a>

  <a href="/tags/#Asp.Net" class='tag'>Asp.Net</a>


          
        </div>
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>这篇文章接着 <a href="/2018/03/27/abp11/">在 ABP 2.3 和 Vue 单页应用中的踩坑总结</a> 来写。</p>
<p>我当时在魔改 ABP 通知系统的时候读了一下它的源码，现在把总结写在这里。</p>
<p>注： 本文涉及的abp版本为 2.3.0，在更新的版本中可能会有出入。</p>
<h1 id="类与表名的对应"><a href="#类与表名的对应" class="headerlink" title="类与表名的对应"></a>类与表名的对应</h1><p>ABP的通知Entity有4种类型（有趣的是这个不以Entity结尾），4个类分别对应数据库中的4张表：</p>
<table>
<thead>
<tr>
<th>类(Entity)</th>
<th>表</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>NotificationInfo</td>
<td>AbpNotifications</td>
<td>发给单个用户的通知，里面包含了接收方用户的id</td>
</tr>
<tr>
<td>TenantNotificationInfo</td>
<td>AbpTenantNotifications</td>
<td>发送的某一类型的通知，只保存通知的内容，不保存用户的ID。注意，这个名字起得不大好，其实这个很租户关系不大</td>
</tr>
<tr>
<td>UserNotificationInfo</td>
<td>AbpUserNotifications</td>
<td>User和TenantNotification的映射，将这两个表关联起来，同时记录用户对订阅消息的状态（已读/未读）</td>
</tr>
<tr>
<td>NotificationSubscriptionInfo</td>
<td>AbpNotificationSubscriptions</td>
<td>记录了某某用户订阅了某某<code>NotificationName</code>或者<code>EntityTypeName</code></td>
</tr>
</tbody>
</table>
<p>通知本身分为两种：</p>
<ol>
<li>向某用户单独发送的通知：这种通知只使用 <code>NotificationInfo</code> 来保存。在直接向某一个用户发送通知时，使用这张表来存储。</li>
<li>采用“订阅、发布”模式发送的通知：用户订阅某种类型（或者某个实体）的通知时，首先把订阅信息存储在 <code>NotificationSubscriptionInfo</code> 中。当发布某个类型的通知时，此通知先被写入<br><code>TenantNotificationInfo</code>，然后ABP会根据<code>NotificationSubscriptionInfo</code>自动为每个订阅的用户在<code>UserNotificationInfo</code>中添加记录。通过<code>_userNotificationManager.GetUserNotificationsAsync</code>会根据<code>UserNotificationInfo</code>连接<code>TenantNotificationInfo</code>来获取到数据。</li>
</ol>
<h1 id="数据表说明"><a href="#数据表说明" class="headerlink" title="数据表说明"></a>数据表说明</h1><p>这里就光给我用到的字段添加注释了</p>
<h2 id="AbpNotifications"><a href="#AbpNotifications" class="headerlink" title="AbpNotifications"></a>AbpNotifications</h2><table>
<thead>
<tr>
<th>列名</th>
<th>类型</th>
<th>注释</th>
</tr>
</thead>
<tbody>
<tr>
<td>Id</td>
<td>int</td>
<td></td>
</tr>
<tr>
<td>CreationTime</td>
<td>datetime2(7)</td>
<td></td>
</tr>
<tr>
<td>CreatorUserId</td>
<td>bigint</td>
<td></td>
</tr>
<tr>
<td>Data</td>
<td>nvarchar(MAX)</td>
<td></td>
</tr>
<tr>
<td>DataTypeName</td>
<td>nvarchar(512)</td>
<td></td>
</tr>
<tr>
<td>EntityId</td>
<td>nvarchar(96)</td>
<td></td>
</tr>
<tr>
<td>EntityTypeAssemblyQualifiedName</td>
<td>nvarchar(512)</td>
<td></td>
</tr>
<tr>
<td>EntityTypeName</td>
<td>nvarchar(250)</td>
<td></td>
</tr>
<tr>
<td>ExcludedUserIds</td>
<td>nvarchar(MAX)</td>
<td></td>
</tr>
<tr>
<td>NotificationName</td>
<td>nvarchar(96)</td>
<td></td>
</tr>
<tr>
<td>Severity</td>
<td>tinyint</td>
<td></td>
</tr>
<tr>
<td>TenantIds</td>
<td>nvarchar(MAX)</td>
<td></td>
</tr>
<tr>
<td>UserIds</td>
<td>nvarchar(MAX)</td>
</tr>
</tbody>
</table>
<h2 id="AbpNotificationSubscriptions"><a href="#AbpNotificationSubscriptions" class="headerlink" title="AbpNotificationSubscriptions"></a>AbpNotificationSubscriptions</h2><table>
<thead>
<tr>
<th>列名</th>
<th>类型</th>
<th>注释</th>
</tr>
</thead>
<tbody>
<tr>
<td>Id</td>
<td>uniqueidentifier</td>
<td></td>
</tr>
<tr>
<td>CreationTime</td>
<td>datetime2(7)</td>
<td></td>
</tr>
<tr>
<td>CreatorUserId</td>
<td>bigint</td>
<td>Creator保存的是当前 Session中的用户ID，UserId是在NotificationSubscriptionManager中指定的用户ID。 如果是管理员让某个用户订阅的话，这两个ID就不一样了。</td>
</tr>
<tr>
<td>EntityId</td>
<td>nvarchar(96)</td>
<td></td>
</tr>
<tr>
<td>EntityTypeAssemblyQualifiedName</td>
<td>nvarchar(512)</td>
<td></td>
</tr>
<tr>
<td>EntityTypeName</td>
<td>nvarchar(250)</td>
<td></td>
</tr>
<tr>
<td>NotificationName</td>
<td>nvarchar(96)</td>
<td>消息名，用这个来标识消息的分类</td>
</tr>
<tr>
<td>TenantId</td>
<td>int</td>
<td></td>
</tr>
<tr>
<td>UserId</td>
<td>bigint</td>
<td>订阅者的UserId</td>
</tr>
</tbody>
</table>
<h2 id="AbpTenantNotifications"><a href="#AbpTenantNotifications" class="headerlink" title="AbpTenantNotifications"></a>AbpTenantNotifications</h2><table>
<thead>
<tr>
<th>列名</th>
<th>类型</th>
<th>注释</th>
</tr>
</thead>
<tbody>
<tr>
<td>Id</td>
<td>bigint</td>
<td></td>
</tr>
<tr>
<td>CreationTime</td>
<td>datetime2(7)</td>
<td></td>
</tr>
<tr>
<td>CreatorUserId</td>
<td>bigint</td>
<td></td>
</tr>
<tr>
<td>Data</td>
<td>nvarchar(MAX)</td>
<td>根据DataTypeName动态改变</td>
</tr>
<tr>
<td>DataTypeName</td>
<td>nvarchar(512)</td>
<td></td>
</tr>
<tr>
<td>EntityId</td>
<td>nvarchar(96)</td>
<td></td>
</tr>
<tr>
<td>EntityTypeAssemblyQualifiedName</td>
<td>nvarchar(512)</td>
<td></td>
</tr>
<tr>
<td>EntityTypeName</td>
<td>nvarchar(250)</td>
<td></td>
</tr>
<tr>
<td>NotificationName</td>
<td>nvarchar(96)</td>
<td></td>
</tr>
<tr>
<td>Severity</td>
<td>tinyint</td>
<td></td>
</tr>
<tr>
<td>TenantId</td>
<td>int</td>
</tr>
</tbody>
</table>
<h2 id="AbpUserNotifications"><a href="#AbpUserNotifications" class="headerlink" title="AbpUserNotifications"></a>AbpUserNotifications</h2><table>
<thead>
<tr>
<th>列名</th>
<th>类型</th>
<th>注释</th>
</tr>
</thead>
<tbody>
<tr>
<td>Id</td>
<td>uniqueidentifier</td>
<td></td>
</tr>
<tr>
<td>CreationTime</td>
<td>datetime2(7)</td>
<td></td>
</tr>
<tr>
<td>State</td>
<td>int</td>
<td>状态</td>
</tr>
<tr>
<td>TenantId</td>
<td>int</td>
<td></td>
</tr>
<tr>
<td>TenantNotificationId</td>
<td>uniqueidentifier</td>
<td>订阅消息ID</td>
</tr>
<tr>
<td>UserId</td>
<td>bigint</td>
<td>用户ID</td>
</tr>
</tbody>
</table>
<h1 id="接口说明"><a href="#接口说明" class="headerlink" title="接口说明"></a>接口说明</h1><ul>
<li>通过 UserNotificationManager 获得的是 UserNotification，访问其 Notification 属性才能得到 TenantNotification</li>
<li>通过 UserNotificationManager 输入 id 来获取notification时，需要输入 UserNotification 的id</li>
<li>租户：UserNotification需要输入 UserIdentifier，这个里面的租户id只用于 UnitOfWork 的 SetTenantId，用来筛选上述的三种通知</li>
</ul>
<h1 id="相关源码"><a href="#相关源码" class="headerlink" title="相关源码"></a>相关源码</h1><ul>
<li><a href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/UserNotificationManager.cs" target="_blank" rel="noopener">https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/UserNotificationManager.cs</a></li>
<li><a href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/bc266e3503fcb2825684ec829cc356815f94f397/src/Abp.Zero.Common/Notifications/NotificationStore.cs" target="_blank" rel="noopener">https://github.com/aspnetboilerplate/aspnetboilerplate/blob/bc266e3503fcb2825684ec829cc356815f94f397/src/Abp.Zero.Common/Notifications/NotificationStore.cs</a></li>
<li><a href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/UserNotificationInfoExtensions.cs" target="_blank" rel="noopener">https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/UserNotificationInfoExtensions.cs</a></li>
<li><a href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/UserNotificationInfoWithNotificationInfoExtensions.cs" target="_blank" rel="noopener">https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/UserNotificationInfoWithNotificationInfoExtensions.cs</a></li>
<li><a href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/UserNotificationInfoWithNotificationInfo.cs" target="_blank" rel="noopener">https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/UserNotificationInfoWithNotificationInfo.cs</a></li>
</ul>

      </section>
      <div class="license">
        <a href="http://creativecommons.org/licenses/by-sa/3.0/" rel="license" target="_blank">
          <img src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" style="border-width:0"
               alt="Creative Commons License" class="center">
        </a>
      </div>
      <p>
        本作品采用
        <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank">
          署名-相同方式共享 4.0 国际
        </a>
        进行许可。欢迎转载、使用、重新发布，但务必保留文章署名
        “不科学的科学君” (Liu233w) 与博客链接：
        <a href="https://liu233w.github.io">https://liu233w.github.io</a>
        ，基于本文修改后的作品务必以相同的许可发布。如有任何疑问，请
        <a href="mailto:wwwlsmcom@outlook.com" target="_blank">与我联系</a>
        。
      </p>
    </article>
    
    <!-- disqus 评论框 start -->
    <div class="comment">
      <div id="disqus_thread" class="disqus-thread">
        <i>加载评论框需要翻墙</i>
      </div>
    </div>
    <!-- disqus 评论框 end -->
    
    
    <!-- livere 评论框 start -->
    <div class="comment">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNTE5NS8xMTczMA=="></div>
    </div>
    <!-- livere 评论框 end -->
    
  </div>
  <aside>
    
    <div class="toc-container">
      <h1>目录</h1>
      <div class="content">
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#类与表名的对应"><span class="toc-number">2.</span> <span class="toc-text">类与表名的对应</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#数据表说明"><span class="toc-number">3.</span> <span class="toc-text">数据表说明</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#AbpNotifications"><span class="toc-number">3.1.</span> <span class="toc-text">AbpNotifications</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#AbpNotificationSubscriptions"><span class="toc-number">3.2.</span> <span class="toc-text">AbpNotificationSubscriptions</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#AbpTenantNotifications"><span class="toc-number">3.3.</span> <span class="toc-text">AbpTenantNotifications</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#AbpUserNotifications"><span class="toc-number">3.4.</span> <span class="toc-text">AbpUserNotifications</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#接口说明"><span class="toc-number">4.</span> <span class="toc-text">接口说明</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#相关源码"><span class="toc-number">5.</span> <span class="toc-text">相关源码</span></a></li></ol>
      </div>
    </div>
    
  </aside>
</main>

<!-- disqus 公共JS代码 -->
<script type="text/javascript">
  /* * * CONFIGURATION VARIABLES * * */
  var disqus_shortname = "liu233w";
  var disqus_identifier = "https://liu233w.github.io/2018/04/02/abp23-notification/";
  var disqus_url = "https://liu233w.github.io/2018/04/02/abp23-notification/";

  isAgent(getDisqus)

  // determine user agent in China
  function isAgent(cb) {
    var url = '//graph.facebook.com/feed?callback=h';
    var xhr = new XMLHttpRequest();
    var called = false;
    xhr.open('GET', url);
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && xhr.status === 200) {
        called = true;
        cb(true);
      }
    };
    xhr.send();
    // timeout 1s, this facebook API is very fast.
    setTimeout(function () {
      if (!called) {
        xhr.abort();
        cb(false)
      }
    }, 1000);
  }

  function getDisqus(isAgent) {
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq)
  }
</script>
<!-- disqus 公共JS代码 end -->


<script type="text/javascript">
  (function (d, s) {
    var j, e = d.getElementsByTagName(s)[0];

    if (typeof LivereTower === 'function') {
      return;
    }

    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;

    e.parentNode.insertBefore(j, e);
  })(document, 'script');
</script>


  <footer>
  <div class="copyright">
    <div>
      &copy; 2019 | Powered by <a href="https://hexo.io" target="_blank">Hexo</a>&nbsp
    </div>
    <div>
      Theme by <a href="https://github.com/lewis-geek/hexo-theme-Aath" target="_blank">Aath</a>
    </div>
  </div>
</footer>


<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="/lib/in-view.min.js"></script>
<script src="/lib/lodash.min.js"></script>
<script>
  var isDown = true
  var oldY = 0
  inView.offset(50)

  document.body.addEventListener('touchstart', function(){});
  
  window.addEventListener('scroll', _.throttle(e => {
    var currentY = window.scrollY
    if((oldY - currentY) < 0) {
      isDown = true
    } else {
      isDown = false
    }
    oldY = currentY
  }, 250))

  $("article img").each(function() {
      var strA = "<a data-fancybox='gallery' href='" + this.src + "'></a>";
      $(this).wrapAll(strA);
  });

  $('.toc-link').each(function() {
      var href = $(this).attr("href");
      
      inView(href).on('exit', () => {
        if (isDown) {
          handleActive(href)
        }
      })

      inView(href).on('enter', () => {
        if (!isDown) {
          handleActive(href)
        }
      })

      this.onclick = function(e) {
        var pos = $(href).offset().top - 10;
        $("html,body").animate({scrollTop: pos}, 300);
        setTimeout(() => {
          handleActive(href)
        }, 350)
        return false
      }
  })

  function handleActive(href) {
    document.querySelectorAll('.toc-link').forEach(elm => {
      elm.classList.remove('active')
    })
    document.querySelector(".toc [href='"+ href +"']").classList.add('active')
  }
</script>
<script src="/lib/jquery.fancybox.min.js"></script>

<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

</body>
</html>
